package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.common.yunos.qiniuyun.UploadUtil;
import com.zcf.mapper.LogMapper;
import com.zcf.mapper.RidingMapper;
import com.zcf.pojo.Dining;
import com.zcf.pojo.Evaluate;
import com.zcf.pojo.Jisuan;
import com.zcf.pojo.Riding;
import com.zcf.pojo.RidingOrder;
import com.zcf.pojo.User;
import com.zcf.service.RidingService;
import com.zcf.utils.UploadImgUtils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/riding")
@CrossOrigin
@ResponseBody
public class RidingController {
	@Autowired
	private RidingService ridingService;
	@Autowired
	private RidingMapper ridingMapper;
	
	/***
	 * 骑手收入
	 * @param ridId
	 * @return
	 */
	@PostMapping("ridShouRu")
	@ResponseBody
	public Body ridShouRu(String id) {
		
		return ridingService.ridShouRu(id);
	}

	// 骑手认证
	@PostMapping("/change")
	public Body change(String phone, String name, Integer age, String sex, String zheng, String fan, String userId) {
		return ridingService.change(phone, name, age, sex, zheng, fan, userId);
	}

	// 上传正面照
	@PostMapping("/setZheng")
	public Body setZheng(MultipartFile[] zurl) {
		if (zurl != null && !"".equals(zurl)) {
			try {
				String zheng = UploadImgUtils.uploadFiles(zurl);
				return Body.newInstance(zheng);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Body.newInstance(501, "照片不能为空");
	}

	/**
	 * 上传多张图片
	 * 
	 * @throws Exception
	 */
	@PostMapping("more")
	public Body more(MultipartFile[] file) throws Exception {
		if (file != null && file.length > 0) {
			String url = UploadImgUtils.uploadFiles(file);
			String[] path = url.split(",");
			String st = "";
			for (int i = 0; i < path.length; i++) {
				if (i == 0) {
					st = path[i];
				} else {
					st = st + "," + path[i];
				}
			}
			return Body.newInstance(st);
		}
		return Body.BODY_500;
	}

	// 上传反面照
	@PostMapping("/setFan")
	public Body setFan(MultipartFile[] furl) {
		if (furl != null && !"".equals(furl)) {
			try {
				String fan = UploadImgUtils.uploadFiles(furl);
				return Body.newInstance(fan);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Body.newInstance(501, "照片不能为空");

	}

	// 查看骑手认证列表
	@PostMapping("/checkList")
	public Body checkList(Integer status) {
		List<Riding> list = ridingMapper.checkList(status);
		if (!list.isEmpty()) {
			return Body.newInstance(list);
		}
		return Body.newInstance(501, "目前没有骑手认证列表");
	}

	// 认证骑手通过
	@PostMapping("/changeStatus")
	public Body changeStatus(String id) {
		return ridingService.changeStatus(id);
	}

	/**
	 * 拒绝通过
	 */
	@PostMapping("refuseStatus")
	public Body refuseStatus(String id, String reason) {
		return ridingService.refuseStatus(id, reason);
	}

	/**
	 * 查询单个骑手
	 */
	@PostMapping("addRidingOne")
	public Body addRidingOne(String id) {
		Riding riding = new Riding();
		riding.setId(id);
		riding = riding.selectById();
		if (riding != null) {
			return Body.newInstance(riding);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询单个骑手
	 */
	@PostMapping("addRidingOneStatus")
	public Body addRidingOneStatus(String userId) {
		EntityWrapper<Riding> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		Riding riding = new Riding();
		riding = riding.selectOne(ew);
		if (riding != null) {
			if (riding.getStart_finish() == 1) {
				riding.setStart("开工中");
			} else if (riding.getStart_finish() == 2) {
				riding.setStart("停工中");
			}
			return Body.newInstance(riding);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询骑手在线人数
	 * 
	 * @return
	 */
	@PostMapping("RidingCount")
	public Body RidingCount() {
		EntityWrapper<Riding> ew = new EntityWrapper<>();
		ew.eq("start_finish", "1");
		ew.eq("status", 1);
		Riding riding = new Riding();
		// 在线骑手数量
		int count = riding.selectCount(ew);

		EntityWrapper<Riding> ews = new EntityWrapper<>();
		ews.eq("start_finish", "2");
		ew.eq("status", 1);
		int couny = riding.selectCount(ews);

		Jisuan jisuan = new Jisuan();
		jisuan.setZaixianCount(count);
		jisuan.setBuzaixianCount(couny);
		return Body.newInstance(jisuan);
	}

	/**
	 * 查询骑手
	 */
	@PostMapping("queryList")
	public Body queryList(String phone, String name, String start) {
		Riding riding = new Riding();
		EntityWrapper<Riding> ew = new EntityWrapper<>();
		ew.eq("status", 1);
		if (phone != null) {
			ew.like("phone", phone);
		}
		if (name != null) {
			ew.like("name", name);
		}
		if (start != null && start != "") {
			ew.eq("start_finish", start);
		}
		List<Riding> list = riding.selectList(ew);
		List<Riding> lists = new ArrayList<>();
		for (Riding riding2 : list) {
			if (riding2.getStart_finish() == 1) {
				riding2.setStart("开工");
			} else if (riding2.getStart_finish() == 2) {
				riding2.setStart("停工");
			}
			lists.add(riding2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询申请的骑手
	 */
	@PostMapping("queryListOne")
	public Body queryListOne(String phone, String name) {
		Riding riding = new Riding();
		EntityWrapper<Riding> ew = new EntityWrapper<>();
		ew.eq("status", 0);
		if (phone != null) {
			ew.like("phone", phone);
		}
		if (name != null) {
			ew.like("name", name);
		}

		List<Riding> list = riding.selectList(ew);
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}

	/**
	 * 删除骑手
	 */
	@PostMapping("delete")
	public Body delete(String id) {
		String[] appid = id.split(",");
		Boolean flag = false;
		Riding din = new Riding();
		for (int i = 0; i < appid.length; i++) {
			din.setId(appid[i]);
			flag = din.deleteById();
		}
		if (flag == true) {
			return Body.newInstance("删除成功");
		}
		return Body.newInstance("删除失败");
	}

	/**
	 * 骑手通过审核
	 */
	@PostMapping("updatechangeStatus")
	public Body updatechangeStatus(String id) {
		String[] appid = id.split(",");
		Boolean flag = false;
		Riding din = new Riding();
		for (int i = 0; i < appid.length; i++) {
			din.setId(appid[i]);
			din = din.selectById();
			din.setStatus(1);
			flag = din.updateById();

		}
		if (flag == true) {
			User user = new User();
			user.setId(din.getUserId());
			user.setStatus(1);
			user.updateById();
			return Body.newInstance("审核成功");
		}
		return Body.newInstance("审核失败");
	}

	/**
	 * 骑手拒绝通过审核
	 */
	@PostMapping("updaterefuseStatus")
	public Body updaterefuseStatus(String id) {
		String[] appid = id.split(",");
		Boolean flag = false;
		Riding din = new Riding();
		for (int i = 0; i < appid.length; i++) {
			din.setId(appid[i]);
			din = din.selectById();
			din.setStatus(2);
			flag = din.updateById();
		}
		if (flag == true) {
			return Body.newInstance("删除成功");
		}
		return Body.newInstance("删除失败");
	}

	/**
	 * 骑手开工停工
	 */
	@PostMapping("update1")
	public Body update1(String userId, int type) {
		User user = new User();
		user = user.selectById(userId);
		if (user != null) {
			EntityWrapper<Riding> ew = new EntityWrapper<>();
			ew.eq("user_id", userId);
			Riding riding = new Riding();
			riding = riding.selectOne(ew);
			if (riding != null) {
				Boolean falg = false;
				if (type == 1) {
					riding.setStart_finish(1);
					falg = riding.updateById();
				} else if (type == 2) {
					riding.setStart_finish(2);
					falg = riding.updateById();
				}
				if (falg) {
					return Body.newInstance(riding);
				}
				return Body.BODY_500;
			}
			return Body.newInstance("骑手不存在");
		}
		return Body.newInstance("骑手不存在");
	}

	/**
	 * 实时定位 所需骑手信息
	 * 
	 * @return
	 */
	@PostMapping("queryOneSerivec")
	public Body n(String orderInfoId) {
		EntityWrapper<RidingOrder> ew = new EntityWrapper<>();
		ew.eq("order_info_id", orderInfoId);
		RidingOrder ridingOrder = new RidingOrder();
		ridingOrder = ridingOrder.selectOne(ew);
		if (ridingOrder != null) {
			Riding riding = new Riding();
			riding = riding.selectById(ridingOrder.getRidingId());
			if (riding != null) {
				return Body.newInstance(riding);
			}
			return Body.BODY_451;
		}
		return Body.BODY_451;
	}

}
